package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.IdeaComment;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class ICDao {

	DBHelp<IdeaComment> db=new DBHelp<IdeaComment>();
	
	public void save(IdeaComment ic){
		String sql="insert into t_ideacomment(id,content,createtime,employeeid,ideaid) values(?,?,?,?,?)";
		db.executeSQL(sql,ic.getId(),ic.getContent(),ic.getCreatetime(),ic.getEmployeeid(),ic.getIdeaid());
	}
	
	public void delete(String id){
		String sql="delete from t_ideacomment where id=?";
		db.executeSQL(sql,id);
	}
	
	public IdeaComment findById(String id){
		String sql="select * from t_ideacomment where id=?";
		return db.queryForObject(sql,new ICRowMapper(),id);
	}
	
	public List<IdeaComment> findByIdeaid(String ideaid){
		String sql="select * from t_ideacomment where ideaid=? order by createtime desc";
		return db.queryForList(sql,new ICRowMapper(),ideaid);
	}
	
	private class ICRowMapper implements RowMapper<IdeaComment>{

		public IdeaComment mapperRow(ResultSet rs) throws SQLException {
			
			IdeaComment ic=new IdeaComment();
			ic.setContent(rs.getString("content"));
			ic.setCreatetime(rs.getString("createtime"));
			ic.setEmployeeid(rs.getInt("employeeid"));
			ic.setId(rs.getString("id"));
			ic.setIdeaid(rs.getString("ideaid"));
			return ic;
			
		}
		
	}
}
